package it.stasbranger.myproject.dao;

import it.stasbranger.myproject.domain.Suppliers;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage Suppliers entities.
 * 
 */
@Repository("SuppliersDAO")
@Transactional
public class SuppliersDAOImpl extends AbstractJpaDao<Suppliers> implements
		SuppliersDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Suppliers.class }));

	/**
	 * EntityManager injected by Spring for persistence unit 
	 *
	 */
	@PersistenceContext(unitName = "")
	private EntityManager entityManager;

	/**
	 * Instantiates a new SuppliersDAOImpl
	 *
	 */
	public SuppliersDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findSuppliersByNameContaining
	 *
	 */
	@Transactional
	public Set<Suppliers> findSuppliersByNameContaining(String name) throws DataAccessException {

		return findSuppliersByNameContaining(name, -1, -1);
	}

	/**
	 * JPQL Query - findSuppliersByNameContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Suppliers> findSuppliersByNameContaining(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSuppliersByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Suppliers>(query.getResultList());
	}

	/**
	 * JPQL Query - findSuppliersByPrimaryKey
	 *
	 */
	@Transactional
	public Suppliers findSuppliersByPrimaryKey(Integer idsuppliers) throws DataAccessException {

		return findSuppliersByPrimaryKey(idsuppliers, -1, -1);
	}

	/**
	 * JPQL Query - findSuppliersByPrimaryKey
	 *
	 */

	@Transactional
	public Suppliers findSuppliersByPrimaryKey(Integer idsuppliers, int startResult, int maxRows) throws DataAccessException {
		try {
			return executeQueryByNameSingleResult("findSuppliersByPrimaryKey", idsuppliers);
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findSuppliersByAddressContaining
	 *
	 */
	@Transactional
	public Set<Suppliers> findSuppliersByAddressContaining(String address) throws DataAccessException {

		return findSuppliersByAddressContaining(address, -1, -1);
	}

	/**
	 * JPQL Query - findSuppliersByAddressContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Suppliers> findSuppliersByAddressContaining(String address, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSuppliersByAddressContaining", startResult, maxRows, address);
		return new LinkedHashSet<Suppliers>(query.getResultList());
	}

	/**
	 * JPQL Query - findSuppliersByIdsuppliers
	 *
	 */
	@Transactional
	public Suppliers findSuppliersByIdsuppliers(Integer idsuppliers) throws DataAccessException {

		return findSuppliersByIdsuppliers(idsuppliers, -1, -1);
	}

	/**
	 * JPQL Query - findSuppliersByIdsuppliers
	 *
	 */

	@Transactional
	public Suppliers findSuppliersByIdsuppliers(Integer idsuppliers, int startResult, int maxRows) throws DataAccessException {
		try {
			return executeQueryByNameSingleResult("findSuppliersByIdsuppliers", idsuppliers);
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findSuppliersByName
	 *
	 */
	@Transactional
	public Set<Suppliers> findSuppliersByName(String name) throws DataAccessException {

		return findSuppliersByName(name, -1, -1);
	}

	/**
	 * JPQL Query - findSuppliersByName
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Suppliers> findSuppliersByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSuppliersByName", startResult, maxRows, name);
		return new LinkedHashSet<Suppliers>(query.getResultList());
	}

	/**
	 * JPQL Query - findSuppliersByAddress
	 *
	 */
	@Transactional
	public Set<Suppliers> findSuppliersByAddress(String address) throws DataAccessException {

		return findSuppliersByAddress(address, -1, -1);
	}

	/**
	 * JPQL Query - findSuppliersByAddress
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Suppliers> findSuppliersByAddress(String address, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSuppliersByAddress", startResult, maxRows, address);
		return new LinkedHashSet<Suppliers>(query.getResultList());
	}

	/**
	 * JPQL Query - findAllSupplierss
	 *
	 */
	@Transactional
	public Set<Suppliers> findAllSupplierss() throws DataAccessException {

		return findAllSupplierss(-1, -1);
	}

	/**
	 * JPQL Query - findAllSupplierss
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Suppliers> findAllSupplierss(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllSupplierss", startResult, maxRows);
		return new LinkedHashSet<Suppliers>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Suppliers entity) {
		return true;
	}
}
